package com.pro.common.modules.service.dependencies.mq;

import cn.hutool.json.JSONUtil;
import com.pro.common.api.model.request.pay.IEnumPaymentBizType;
import com.pro.common.api.model.mq.PaymentNotifyDataBusiness;
import com.pro.common.modules.service.dependencies.service.BaseOrderPayService;
import com.pro.framework.api.util.CollUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

//@Service
@Slf4j
public abstract class BasePayNotifyMqConsumer {
    Map<IEnumPaymentBizType, ? extends BaseOrderPayService< ?, ?>> serviceMap;

    @Autowired(required = false)
    public void setServiceList(List<BaseOrderPayService< ?, ?>> serviceList) {
        this.serviceMap = CollUtils.listToMap(serviceList, BaseOrderPayService::getBizType);
    }

    public Map<String, ? extends IEnumPaymentBizType> getBizTypeMap() {
        return Collections.emptyMap();
    }

    /**
     * 消费用户注册的事件，处理相关业务逻辑
     */
    public void handlePayNotify(PaymentNotifyDataBusiness data) {
        log.info("handlePayNotify {}", JSONUtil.toJsonStr(data));
        BaseOrderPayService< ?, ?> service = serviceMap.get(getBizTypeMap().get(data.getBizType()));
        if (data.getStatusSuccess()) {
            service.createOrderPayCallbackSuccess(data.getPayAmount(), data.getBizId());
        }
        if (data.getStatusFail()) {
            service.createOrderPayCallbackFail(data.getBizId());
        }
    }
}
